package com.mini.core.grammar;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.mini.core.BaseEntity;

/**
 * 完全拷贝RecordRowMapper，修改mapRow方法
 * 有个前提，所有的Bean必须继承BaseEntity
 * 
 * @author sxjun
 * @param <T>
 * @date 2015-12-29 下午10:00:39 
 *
 */
public class BaseEntityRowMapper<T> implements RowMapper<T> {
	private Class<? extends BaseEntity> clazz;
	public BaseEntityRowMapper(Class clazz){
		this.clazz = clazz;
	}

	public T mapRow(ResultSet rs, int rowNum) throws SQLException {
		BaseEntity o = null;
		try {
			ResultSetMetaData meta = rs.getMetaData();
			int columns = meta.getColumnCount();
			o = clazz.newInstance();
			o.setPrimaryKeys(o.getPrimaryKeys());
			o.setTableName(o.getTableName());
			for(int i=1;i<=columns;i++)
			    o.set(meta.getColumnName(i), rs.getObject(meta.getColumnName(i)));
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
        return (T) o;
	}
}
